package com.yanzhenjie.andserver;

import com.yanzhenjie.andserver.util.FileUtils;
import com.yanzhenjie.andserver.util.IOUtils;
import ohos.aafwk.ability.AbilityPackage;
import ohos.app.Context;
import org.greenrobot.eventbus.EventBus;

import java.io.File;

public class MyApplication extends AbilityPackage {
    private static MyApplication mInstance;

    private File mRootDir;

    @Override
    public void onInitialize() {
        super.onInitialize();
        if (mInstance == null) {
            mInstance = this;
            initRootPath(this);
        }
        EventBus.initThreadForHos(getUITaskDispatcher());
    }

    public static MyApplication getInstance() {
        return mInstance;
    }

    public File getRootDir() {
        return mRootDir;
    }

    private void initRootPath(Context context) {
        if (mRootDir != null) {
            return;
        }

        if (FileUtils.storageAvailable(context)) {
            mRootDir = context.getDistributedDir();
        } else {
            mRootDir = context.getFilesDir();
        }
        mRootDir = new File(mRootDir, "AndServer");
        IOUtils.createFolder(mRootDir);
    }
}
